package com.sp.scmdb.serviceImpl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.interceptor.Fault;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.sp.scmdb.bean.EmployeeBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.AreaDao;
import com.sp.scmdb.dao.BuMenDao;
import com.sp.scmdb.dao.CallAlarmDao;
import com.sp.scmdb.dao.DepartmentDao;
import com.sp.scmdb.dao.DutyDao;
import com.sp.scmdb.dao.EmpDayReportDao;
import com.sp.scmdb.dao.EmployeeDao;
import com.sp.scmdb.dao.RelEmpAreaDao;
import com.sp.scmdb.dao.ShiftsDao;
import com.sp.scmdb.dao.TagDao;
import com.sp.scmdb.dao.TitleDao;
import com.sp.scmdb.dao.WorkTimeDao;
import com.sp.scmdb.dao.WorkTypeDao;
import com.sp.scmdb.entity.Area;
import com.sp.scmdb.entity.BuMen;
import com.sp.scmdb.entity.Department;
import com.sp.scmdb.entity.Duty;
import com.sp.scmdb.entity.EmpDayReport;
import com.sp.scmdb.entity.Employee;
import com.sp.scmdb.entity.RelEmpArea;
import com.sp.scmdb.entity.Shifts;
import com.sp.scmdb.entity.Tag;
import com.sp.scmdb.entity.Title;
import com.sp.scmdb.entity.WorkTime;
import com.sp.scmdb.entity.WorkType;
import com.sp.scmdb.service.EmployeeService;
import com.sp.scmdb.service.LogService;
import com.sp.scmdb.spring.ApplicationContext;
import com.sp.scmdb.webservice.model.EmpModel;
import com.sp.scmdb.webservice.model.ReturnResponse;
import com.sp.scmdb.webservice.service.UploadDataService;
import com.sp.scmdb.webservice.suport.CastorSerializer;

@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {

	private static Log log = LogFactory.getLog(ReaderServiceImpl.class);
	private EmployeeDao employeeDao;

	private DutyDao dutyDao;

	private TitleDao titleDao;

	private WorkTimeDao workTimeDao;

	private DepartmentDao departmentDao;

	private RelEmpAreaDao relEmpAreaDao;

	private AreaDao areaDao;

	private TagDao tagDao;
	
	private BuMenDao bumenDao;

	private ShiftsDao shiftsDao;

	private WorkTypeDao workTypeDao;

	private EmpDayReportDao empDayReportDao;
	
	private LogService logs;
	
	private CallAlarmDao callAlarmDao;

	public static void setLog(Log log) {
		EmployeeServiceImpl.log = log;
	}

	public void setBumenDao(BuMenDao bumenDao) {
		this.bumenDao = bumenDao;
	}

	@Autowired
	public void setCallAlarmDao(CallAlarmDao callAlarmDao) {
		this.callAlarmDao = callAlarmDao;
	}

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true,isolation=Isolation.READ_UNCOMMITTED)
	public EmployeeBean queryAll(EmployeeBean empBean, EmployeeBean oldBean, UserBean user) {
		
		empBean.setListDuty(this.getListDuty());
		empBean.setListTitle(this.getListTitle());
		empBean.setListDepartment(this.getListDepartment(user));
		empBean.setListWorkType(this.getListWorkType());
		empBean.setListBuMen(this.getListBuMen());
		List<Employee> list = null;
		if (empBean.equals(oldBean)) {
			list = oldBean.getAllList();
		} else {
			//StringBuffer Hql = new StringBuffer("from Employee e  where 1=1 "); //把phone查询出来程序会报异常
			StringBuffer Hql = new StringBuffer("select new Employee(e.id, e.empName, e.empLableID, e.empWorkID, " +
					"e.empSex, e.depart, e.workType, e.title, e.duty, e.workTime, e.shifts,e.bumen, " +
					"e.maxWorkTime) " +
					"from Employee as e " +
					" left join e.depart depart " +
					" left join e.workType workType" +
					" left join e.duty duty " +
					" left join e.workTime workTime " +
					" left join e.shifts shifts" +
					" left join e.title title " +
					" left join e.maxWorkTime maxWorkTime " +
					" left join e.bumen bumen " +
					"where 1=1 ");

			if (StringUtils.isNotEmpty(empBean.getEmpLabelID())) {
				Hql.append("and e.empLableID like'%").append(empBean.getEmpLabelID().trim()).append("%' ");
			}
			if (StringUtils.isNotEmpty(empBean.getEmpName())) {
				Hql.append("and e.empName like '%").append(empBean.getEmpName().trim()).append("%' ");
			}
			if (StringUtils.isNotEmpty(empBean.getEmpDepID()) && !"0".equals(empBean.getEmpDepID())) {
				Hql.append("and e.empDepID =").append(empBean.getEmpDepID()).append(" ");
			}
			if (StringUtils.isNotEmpty(empBean.getEmpWorkTypeID()) && !"0".equals(empBean.getEmpWorkTypeID())) {
				Hql.append("and e.empWorkTypeID =").append(empBean.getEmpWorkTypeID()).append(" ");
			}
			if (StringUtils.isNotEmpty(empBean.getEmpBuMenID()) && !"0".equals(empBean.getEmpBuMenID())) {
				Hql.append("and e.empBuMenID = ").append(empBean.getEmpBuMenID()).append(" ");
			}
			if (StringUtils.isNotEmpty(empBean.getIsSpecialEmp())) {
				// 是否特殊工种
				Hql.append("and e.empType=").append(empBean.getIsSpecialEmp()).append(" ");
			}
			try {
				list = employeeDao.queryHql(Hql.toString());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (CollectionUtils.isNotEmpty(list)) {
			empBean.setAllList(list);
			empBean.setList(list.subList(empBean.getCurrentRow(), empBean.getLastRow()));
		}
		return empBean;
	}
	public List<EmployeeBean> getEmployeeList(String hql) {
		EmployeeBean employeeBean = null;
		Employee employee = null;
		String _hql = "select new Employee(e.id, e.empName, e.empLableID, e.unitName) from Employee as e where 1 = 1";
		_hql += hql;
		List<Employee> list = employeeDao.queryHql(_hql);
		List<EmployeeBean> employeeList = new ArrayList<EmployeeBean>();
		try {
			for(int i = 0; i < list.size(); i ++) {
				employee = list.get(i);
				employeeBean = new EmployeeBean();
				employeeBean.setId(employee.getId().toString());
				employeeBean.setEmpName(employee.getEmpName());
				employeeBean.setEmpLabelID(employee.getEmpLableID());
				employeeBean.setUnitName(employee.getUnitName());
				employeeList.add(employeeBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return employeeList;
	}

	@Transactional
	public String newEmp(EmployeeBean bean, UserBean user) {

		// 检测卡号或者工号是否重复
		List<Tag> listTag = tagDao.queryHql("from Tag where tagno = '" + bean.getEmpLabelID() + "'");
		StringBuffer Hql;
		Hql = new StringBuffer("from Employee where empLableID='").append(bean.getEmpLabelID()).append("'");
		if (CollectionUtils.isNotEmpty(employeeDao.queryHql(Hql.toString()))) {
			// 卡号重复
			return "10";
		}
		Hql = new StringBuffer("from Tag where usestat = '1' and tagno = '" + bean.getEmpLabelID() + "'");
		if (CollectionUtils.isNotEmpty(tagDao.queryHql(Hql.toString())) || CollectionUtils.isEmpty(listTag)) {
			// 卡号已使用或者卡号不存在
			return "20";
		}

		// 卡号、工号均不重复
		Employee emp = new Employee();
		emp.setEmpAddr(bean.getEmpAddr());
		emp.setEmpDepID(bean.getEmpDepID());
		emp.setEmpDutyID(bean.getEmpDutyID());
		emp.setEmpLableID(bean.getEmpLabelID());
		emp.setEmpName(bean.getEmpName());
		InputStream in = null;
		if (StringUtils.isNotEmpty(bean.getEmpPhotoFileName())) {
			try {
				byte buffer[] = new byte[(int)bean.getEmpPhoto().length()]; 
		        in = new FileInputStream(bean.getEmpPhoto()); 
		        in.read(buffer); 
			    emp.setEmpPhoto(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		emp.setEmpPhone(bean.getEmpPhone());
		emp.setIdentityNo(bean.getIdentityNo());
		emp.setEmpSex(bean.getEmpSex());
		emp.setEmpTitleID(bean.getEmpTitleID());
		emp.setEmpWorkID(bean.getEmpWorkID());
		emp.setEmpShiftID(bean.getEmpShiftsID());
		emp.setEmpWorkTime(bean.getEmpWorkTime());
		emp.setEmpMaxWorkTime(bean.getEmpMaxWorkTime());
		emp.setEmpType(bean.getEmpType());
		emp.setEmpWorkTypeID(bean.getEmpWorkTypeID());
		emp.setReID(bean.getReID());
		emp.setPeccancys(bean.getPeccancys());
		emp.setProName(bean.getProName());
		emp.setProID(bean.getProID());
		emp.setUnitName(bean.getUnitName());
		emp.setEntryTime(bean.getEntryTime());
		emp.setSpection(bean.getSpection());
		emp.setContract(bean.getContract());
		emp.setContractTime(bean.getContractTime());
		emp.setCertificate(bean.getCertificate());
		emp.setCertificateName(bean.getCertificateName());
		emp.setTownSecure(bean.getTownSecure());
		emp.setOtherSecure(bean.getOtherSecure());
		emp.setSafeCard(bean.getSafeCard());
		emp.setCardTime(bean.getCardTime());
		emp.setSafePosts(bean.getSafePosts());
		emp.setPostsTime(bean.getPostsTime());
		emp.setTrack(bean.getTrack());
		emp.setTrackTime(bean.getTrackTime());
		emp.setSafeOption(bean.getSafeOption());
		emp.setSafeOptionTime(bean.getSafeOptionTime());
		emp.setOrderBook(bean.getOrderBook());
		emp.setOrderTime(bean.getOrderTime());
		// 保存人员
		employeeDao.save(emp);
		if (null != in) {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		// 修改卡片使用状态为“已使用”
		Tag tag = listTag.get(0);
		tag.setUsestat("1");
		tagDao.update(tag);	

		// 向考勤表增加一个人员信息
		EmpDayReport empDayReport = new EmpDayReport();
		empDayReport.setEmpID(emp.getId());
		empDayReport.setLabelID(Integer.parseInt(emp.getEmpLableID()));
		empDayReportDao.save(empDayReport);

		// 如果是特种人员，则添加区域及时间信息		
		if ("1".equals(bean.getEmpType())) {
			String[] area = bean.getArea().split(",");
			String[] startTime = bean.getStartTime().split(",");
			String[] endTime = bean.getEndTime().split(",");
			for (int i = 0; i < area.length; i++) {
				RelEmpArea relEmpArea = new RelEmpArea();
				relEmpArea.setIdEmp(emp.getId());
				relEmpArea.setIdArea(Integer.parseInt(area[i].trim()));
				relEmpArea.setStartTime(startTime[i].trim());
				relEmpArea.setEndTime(endTime[i].trim());
				// 如果开始时间小于结束时间，则设置为“不跨天”，否则就是跨天
				if (this.getDate(relEmpArea.getStartTime()).before(this.getDate(relEmpArea.getEndTime()))) {
					relEmpArea.setDayFlag("1");
				} else {
					relEmpArea.setDayFlag("0");
				}

				relEmpAreaDao.save(relEmpArea);
			}
		}
		logs.addLog(user, "添加 人员：\""+bean.getEmpName()+"\"成功！");

		return CommonParamter.CHECK_SUCCESS;
	}

	@Transactional
	public String editEmp(EmployeeBean bean, UserBean user) {
		String result = "3";

		Employee emp = employeeDao.getById(Integer.parseInt(bean.getId()));
		if (emp != null) {

			// 检测卡号或者工号是否重复
			List<Tag> listTag = tagDao.queryHql("from Tag where tagno = '" + bean.getEmpLabelID() + "'");
			StringBuffer Hql;
			Hql = new StringBuffer("from Employee where empLableID='" + bean.getEmpLabelID() + "' and id != "
					+ bean.getId());
			if (CollectionUtils.isNotEmpty(employeeDao.queryHql(Hql.toString()))) {
				// 卡号重复
				return "10";
			}
			if (!bean.getEmpLabelID().equals(emp.getEmpLableID())) {
				// 如果修改了卡号，则要检测该卡号是否存在以及是否未使用
				Hql = new StringBuffer("from Tag where usestat = '1' and tagno = '" + bean.getEmpLabelID() + "'");
				if (CollectionUtils.isNotEmpty(tagDao.queryHql(Hql.toString())) || CollectionUtils.isEmpty(listTag)) {
					// 卡号已使用或者卡号不存在
					return "20";
				}
			}

			// 如果卡号和之前该人员的卡号不同，则先将原先的卡号使用状态置为“未使用”，再将现在卡号使用状态设置为“已使用”
			// 原先的卡号设置为“未使用”
			listTag = tagDao.queryHql("from Tag where tagno = '" + emp.getEmpLableID() + "'");
			if(listTag.size()>0){
				Tag tag = listTag.get(0);
				tag.setUsestat("0");
				tagDao.update(tag);
				// 现在的卡号设置为“已使用”
				listTag = tagDao.queryHql("from Tag where tagno = '" + bean.getEmpLabelID() + "'");
				tag = listTag.get(0);
				tag.setUsestat("1");
				tagDao.update(tag);
			}

			// 修改人员信息
			emp.setEmpAddr(bean.getEmpAddr());
			emp.setEmpDepID(bean.getEmpDepID());
			emp.setEmpDutyID(bean.getEmpDutyID());
			emp.setEmpLableID(bean.getEmpLabelID());
			emp.setEmpShiftID(bean.getEmpShiftsID());
			emp.setEmpName(bean.getEmpName());
			emp.setPeccancys(bean.getPeccancys());
			emp.setEmpPhone(bean.getEmpPhone());
			emp.setIdentityNo(bean.getIdentityNo());
			emp.setBlood(bean.getBlood());
			emp.setEmpBuMenID(bean.getEmpBuMenID());
			InputStream in = null;
			if (StringUtils.isNotEmpty(bean.getEmpPhotoFileName())) {
				try {
					byte buffer[] = new byte[(int)bean.getEmpPhoto().length()]; 
			        in = new FileInputStream(bean.getEmpPhoto()); 
			        in.read(buffer); 
				    emp.setEmpPhoto(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			emp.setEmpSex(bean.getEmpSex());
			emp.setEmpTitleID(bean.getEmpTitleID());
			emp.setEmpWorkID(bean.getEmpWorkID());
			emp.setEmpWorkTime(bean.getEmpWorkTime());
			emp.setEmpMaxWorkTime(bean.getEmpMaxWorkTime());
			emp.setEmpType(bean.getEmpType());
			emp.setEmpWorkTypeID(bean.getEmpWorkTypeID());
			emp.setReID(bean.getReID());
			emp.setSafetys(bean.getSafetys());
			
			emp.setProName(bean.getProName());
			emp.setProID(bean.getProID());
			emp.setUnitName(bean.getUnitName());
			emp.setEntryTime(bean.getEntryTime());
			emp.setSpection(bean.getSpection());
			emp.setContract(bean.getContract());
			emp.setContractTime(bean.getContractTime());
			emp.setCertificate(bean.getCertificate());
			emp.setCertificateName(bean.getCertificateName());
			emp.setTownSecure(bean.getTownSecure());
			emp.setOtherSecure(bean.getOtherSecure());
			emp.setSafeCard(bean.getSafeCard());
			emp.setCardTime(bean.getCardTime());
			emp.setSafePosts(bean.getSafePosts());
			emp.setPostsTime(bean.getPostsTime());
			emp.setTrack(bean.getTrack());
			emp.setTrackTime(bean.getTrackTime());
			emp.setSafeOption(bean.getSafeOption());
			emp.setSafeOptionTime(bean.getSafeOptionTime());
			emp.setOrderBook(bean.getOrderBook());
			emp.setOrderTime(bean.getOrderTime());
			// 保存该人员
			employeeDao.update(emp);
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			// 如果是特种人员，则添加区域及时间信息
			if ("1".equals(bean.getEmpType())) {
				// 首先删除该人员和区域的信息(RelEmpArea)
				String relHql = "from RelEmpArea where idEmp=" + emp.getId();
				List<RelEmpArea> list = relEmpAreaDao.queryHql(relHql);
				RelEmpArea relEmpArea = null;
				for (int i = 0; i < list.size(); i++) {
					relEmpArea = list.get(i);
					if (relEmpArea != null) {
						relEmpAreaDao.delete(relEmpArea);
					}
				}
				
				if (!StringUtils.isEmpty(bean.getArea())){
					String[] area = bean.getArea().split(",");
					String[] startTime = bean.getStartTime().split(",");
					String[] endTime = bean.getEndTime().split(",");
					for (int i = 0; i < area.length; i++) {
						relEmpArea = new RelEmpArea();
						relEmpArea.setIdEmp(emp.getId());
						relEmpArea.setIdArea(Integer.parseInt(area[i].trim()));
						relEmpArea.setStartTime(startTime[i].trim());
						relEmpArea.setEndTime(endTime[i].trim());
						// 如果开始时间小于结束时间，则设置为“不跨天”，否则就是跨天
						if (this.getDate(relEmpArea.getStartTime()).before(this.getDate(relEmpArea.getEndTime()))) {
							relEmpArea.setDayFlag("1");
						} else {
							relEmpArea.setDayFlag("0");
						}

						relEmpAreaDao.save(relEmpArea);
					}
				}				
			} else {
				// 查看该人员是否和区域关联过，如果关联表有数据，则删除该数据
				String relHql = "from RelEmpArea where idEmp=" + emp.getId();
				List<RelEmpArea> list = relEmpAreaDao.queryHql(relHql);
				RelEmpArea relEmpArea = null;
				for (int i = 0; i < list.size(); i++) {
					relEmpArea = list.get(i);
					if (relEmpArea != null) {
						relEmpAreaDao.delete(relEmpArea);
					}
				}
			}

			result = CommonParamter.CHECK_SUCCESS;
		}
		logs.addLog(user, "修改 人员：\""+bean.getEmpName()+"\"添加成功");
		return result;
	}

	@Transactional
	public String delEmp(String[] arr, UserBean user) {
		Iterator<RelEmpArea> iterator = null;
		Employee emp = null;
		RelEmpArea relEmpArea = null;
		for (int i = 0; i < arr.length; i++) {
			emp = employeeDao.getById(Integer.parseInt(arr[i]));
			if (emp != null) {
				// 当前在园内的人员不能删除(OBJ_ENTRYRECORD表中没有该人员的信息)
				if (!employeeDao.findBySql("select * from OBJ_ENTRYRECORD where empID = " + emp.getId()).isEmpty()) {
					return emp.getEmpName() + "(" + emp.getEmpWorkID() + ")还未出园，不能删除！";
				}
				if (emp.getRelEmpAreaList().iterator() != null) {
					// 取出人员区域关联信息列表
					iterator = emp.getRelEmpAreaList().iterator();
					while (iterator.hasNext()) {
						// 删除人员区域关联信息列表
						relEmpArea = iterator.next();
						relEmpAreaDao.delete(relEmpArea);
					}
				}
				// 修改卡片使用状态为“未使用”
				List<Tag> listTag = tagDao.queryHql("from Tag where tagno = '" + emp.getEmpLableID() + "'");
				if (CollectionUtils.isNotEmpty(listTag)) {
					Tag tag = listTag.get(0);
					tag.setUsestat("0");
					tagDao.update(tag);
				}
				//删除人员紧急呼叫信息
				callAlarmDao.excuteUpdate("delete from OBJ_CALLALARM where empID="+emp.getId());
				logs.addLog(user, "删除 人员：\""+emp.getEmpName()+"\"成功");
				employeeDao.delete(emp);
			}
		}
		return CommonParamter.CHECK_SUCCESS;
	}

	/**
	 * 根据id获取人员信息
	 * 
	 * @param id
	 * @return 人员信息
	 */
	@Transactional(readOnly = true)
	public Employee getById(String id) {
		int intid = 0;
		try {
			intid = Integer.parseInt(id);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return employeeDao.getById(intid);
	}

	/**
	 * 根据卡号（多个）取得人员信息
	 */
	@Transactional(readOnly = true,isolation=Isolation.READ_UNCOMMITTED)
	public EmployeeBean getByLableIDs(EmployeeBean empBean, String labelIDs, UserBean user) {
		if (StringUtils.isNotEmpty(labelIDs)) {
			empBean.setListDuty(this.getListDuty());
			empBean.setListTitle(this.getListTitle());
			empBean.setListWorkTime(this.getListWorkTime());
			empBean.setListDepartment(this.getListDepartment(user));

			List<Employee> list = null;
			StringBuffer Hql = new StringBuffer("select new Employee(e.id, e.empName, e.empLableID, e.empWorkID, e.depart) " +
					"from Employee as e " +
					" left join e.depart depart " +
					"where e.empLableID in(" + labelIDs + ")");
			
			list = employeeDao.queryHql(Hql.toString());
			
			if (CollectionUtils.isNotEmpty(list)) {
				empBean.setAllList(list);
				empBean.setList(list.subList(empBean.getCurrentRow(), empBean.getLastRow()));
			}
		}
		
		return empBean;
	}

	public Date getDate(String str) {
		String fmt = "HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(fmt);
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return date;
	}

	public List<Duty> getListDuty() {
		return dutyDao.queryHql("from Duty order by sortindex ");
	}

	public List<Title> getListTitle() {
		return titleDao.queryHql("from Title order by sortindex ");
	}
	public List<BuMen> getListBuMen(){
		return bumenDao.queryHql("from BuMen order by sortindex ");
	}
	public List<WorkTime> getListWorkTime() {
		return workTimeDao.queryHql("from WorkTime where type=0");
	}

	public List<WorkTime> getListMaxWorkTime() {
		return workTimeDao.queryHql("from WorkTime where type=1");
	}

	public List<Shifts> getListShifts() {
		return shiftsDao.queryAll();
	}

	public List<Department> getListDepartment(UserBean user) {
		return departmentDao.queryHql("from Department order by sortindex");
	}

	/**
	 * 获取所有的区域列表
	 */
	public List<Area> getListArea() {

		return areaDao.queryAll();
	}
	
	/**
	 * 获取限制区域列列表
	 * @return 限制区域
	 */
	public List<Area> getRestrictAlarm() {
		
		return areaDao.queryHql("from Area where type = 2");
	}

	/**
	 * 获取所有的未使用卡号
	 */
	public List<Tag> getUnusedTags(String tagNo){
		return tagDao.queryHql("from Tag where usestat = 0 and tagno like '" + tagNo + "%'");
	}
	
	public List<WorkType> getListWorkType() {

		return workTypeDao.queryAll();
	}

	public TitleDao getTitleDao() {
		return titleDao;
	}

	public void setTitleDao(TitleDao titleDao) {
		this.titleDao = titleDao;
	}

	public EmployeeDao getEmployeeDao() {
		return employeeDao;
	}

	public void setDutyDao(DutyDao dutyDao) {
		this.dutyDao = dutyDao;
	}

	public void setWorkTimeDao(WorkTimeDao workTimeDao) {
		this.workTimeDao = workTimeDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public void setRelEmpAreaDao(RelEmpAreaDao relEmpAreaDao) {
		this.relEmpAreaDao = relEmpAreaDao;
	}

	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	public void setTagDao(TagDao tagDao) {
		this.tagDao = tagDao;
	}

	public void setAreaDao(AreaDao areaDao) {
		this.areaDao = areaDao;
	}

	public void setShiftsDao(ShiftsDao shiftsDao) {
		this.shiftsDao = shiftsDao;
	}

	public void setWorkTypeDao(WorkTypeDao workTypeDao) {
		this.workTypeDao = workTypeDao;
	}

	public void setEmpDayReportDao(EmpDayReportDao empDayReportDao) {
		this.empDayReportDao = empDayReportDao;
	}

	@Required
	public void setLogs(LogService logs) {
		this.logs = logs;
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true,isolation=Isolation.READ_UNCOMMITTED)
	public EmployeeBean findUpload(EmployeeBean empBean, EmployeeBean oldBean) {
		List<Employee> list = null;
		if (empBean.equals(oldBean)) {
			list = oldBean.getAllList();
		} else {
			StringBuffer Hql = new StringBuffer("select new Employee(e.id, e.empName, e.empLableID, e.empWorkID, " +
					"e.empSex, e.depart, e.workType, e.duty, e.title, e.workTime, e.shifts, " +
					"e.maxWorkTime) " +
					"from Employee as e " +
					" left join e.depart depart " +
					" left join e.workType workType" +
					" left join e.duty duty " +
					" left join e.workTime workTime " +
					" left join e.shifts shifts" +
					" left join e.title title " +
					" left join e.maxWorkTime maxWorkTime " +
					"where 1=1 and dutyLevelId<4 and dutyLevelId>0");
			try {
				list = employeeDao.queryHql(Hql.toString());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (CollectionUtils.isNotEmpty(list)) {
			empBean.setAllList(list);
			empBean.setList(list.subList(empBean.getCurrentRow(), empBean.getLastRow()));
		}
		return empBean;
	}	

	@SuppressWarnings({ "finally", "unchecked" })
	@Transactional(readOnly = true,isolation=Isolation.READ_UNCOMMITTED)
	public String upload(String id) throws Fault{
		String result = CommonParamter.CHECK_FAIL;
		try{
			String Hql = "from Employee where duty.dutyLevelId<4";
			List<Employee> empList = employeeDao.queryHql(Hql);
			Employee emp = new Employee();
			if(CollectionUtils.isNotEmpty(empList)){
				List<EmployeeBean> listBean = new ArrayList();
				for(int i=0;i<empList.size();i++){
					emp = empList.get(i);
					EmployeeBean eb = new EmployeeBean();
					eb.setId(emp.getId()+"");
					eb.setEmpName(emp.getEmpName());
					eb.setEmpSex(emp.getEmpSex());
					eb.setEmpLabelID(emp.getEmpLableID());
					eb.setEmpWorkID(emp.getEmpWorkID());
					eb.setEmpDutyID(emp.getEmpDutyID());
					eb.setEmpPhone(emp.getEmpPhone());
					eb.setEmpAddr(emp.getEmpAddr());
					eb.setEmpTitleID(emp.getEmpTitleID());
					if(emp.getDuty()==null)
						eb.setDutyName("");
					else
						eb.setDutyName(emp.getDuty().getName());
					if(emp.getTitle()==null)
						eb.setTitleName("");
					else
						eb.setTitleName(emp.getTitle().getName());
					eb.setIdentityNo(emp.getIdentityNo());
					listBean.add(eb);
				}
				EmpModel model = new EmpModel();
				model.setUserId(CommonParamter.userID);
				model.setPassword(CommonParamter.password);
				model.setListBean(listBean);
				
				CastorSerializer serializerTool = new CastorSerializer();
				ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
				serializerTool.addMappingLocation("com/sp/scmdb/webservice/mapping/mapping.xml");
				serializerTool.marshal(model, byteOutputStream, "UTF-8");
				String str = new String(byteOutputStream.toByteArray());
				System.out.println("str=================" + str);
				
				UploadDataService upload=(UploadDataService )ApplicationContext.getBean("uploadService");
				result = upload.uploadEmp(str);
				ReturnResponse response = (ReturnResponse) serializerTool
				.unmarshal(new ByteArrayInputStream(result.getBytes()),
						"GB2312");
				result = response.getDescription();
			}
		}
		catch(javax.xml.ws.WebServiceException e){
			if ("Could not send Message.".equals(e.getMessage())) {
				result = "timeOut";
			}else{
				result = e.getMessage();
			}
		}
		catch(Exception e){
			log.error(e.getMessage());
		}
		finally{
			return result;
		}
	}
	
	@Transactional
	public String editEmployee(Employee employee) {
		String result = CommonParamter.CHECK_FAIL;
		try {
			employeeDao.update(employee);
			result = CommonParamter.CHECK_SUCCESS;
		} catch (Exception e) {
			log.error("update Employee is faild! reason:" + e.getMessage());
		}
		
		return result;
	}
}
